﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
//using System.Xml.Linq;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.DAL;
using System.Collections.Generic;
using KPIS.GERP.GHRMS.APP.Classes;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class TR_TrainingCoursesDesDtls : System.Web.UI.Page
    {
        private SsMng ssMng = new SsMng();
        private Main main = new Main();
        private SsMng creatby = new SsMng();
        InitialData_BLL iniData = new InitialData_BLL();
        IJobSiteBLL jBLL = new JobSiteBLL();
        JobSiteInfo info = new JobSiteInfo();
        ConvertNullable cn = new ConvertNullable();
        TrainingCoursesInfo courseInfo = new TrainingCoursesInfo();
        TrainingCoursesDAL courseDAL = new TrainingCoursesDAL();
        ITrainingCoursesQuotaBLL qBLL = new TrainingCoursesQuotaBLL();
        ITrainingCoursesTrainerBLL tBLL = new TrainingCoursesTrainerBLL();
        ITrainingLecturerSkillsBLL lSkillBLL = new TrainingLecturerSkillsBLL();
        IEmployeeBLL EmpBLL = new EmployeeBLL();
        TrainingCoursesDesDtlsBLL CDesBLL = new TrainingCoursesDesDtlsBLL();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //lblID.Text = Request.QueryString["id"];
                //Session["id"] = Request.QueryString["id"];
                LoadEmployeeList();
                LoadDetail();
                LoadJobSite();
                LoadDataGrid2(); 
                LoadDataGrid1();
                LoadcbTrainee();
            }
            
        }

        protected void LoadDetail()
        {
            int train_seq = Convert.ToInt32(Convert.ToInt32(Request.QueryString["Sequence"].ToString()));
            TrainingCoursesInfo info = courseDAL.GetData(train_seq);
            lbTrainingCourseDescr.Text = info.training_course_descr;
            //lbTotalScore.Text = info.total_score.ToString();
            lbTotalPrice.Text = info.total_price.ToString();
            if (info.date_sign_start.ToString().Length > 0)
            { lbDateSignStard.Text = info.date_sign_start.Value.ToShortDateString(); }
            if (info.date_sign_end.ToString().Length > 0)
            { lbDateSignEnd.Text = info.date_sign_end.Value.ToShortDateString(); }
            if (info.date_training_start.ToString().Length > 0)
            { lbDateTrainStard.Text = info.date_training_start.Value.ToShortDateString(); }
            if (info.date_training_end.ToString().Length > 0)
            { lbDateTrainEnd.Text = info.date_training_end.Value.ToShortDateString(); }  
            lbTotalPrice.Text = info.total_price.ToString();
            //lbTotalScore.Text = info.total_score.ToString();
            lbMaxTrainee.Text = info.max_trainee_count.ToString();
            lbSchedule.Text = info.schedule_text.ToString();
            string TrainSeq = Request.QueryString["Sequence"].ToString();
            DataSet ListDataCommand = CDesBLL.GetData("",TrainSeq,"1");
            if (ListDataCommand.Tables.Count > 0)
            {
                DataTable table = ListDataCommand.Tables[0];
                if (table.Rows.Count > 0)
                { lbQuota.Text = table.Rows[0]["COUNT_QUTA"].ToString(); }
            }
        }
 //======================================== รายชื่อผู้สมัคร  =================================================  
        private void LoadDataGrid2()
       {
            string TrainSeq = Request.QueryString["Sequence"].ToString();
            DataSet ListSalaryCommand = CDesBLL.GetData("", TrainSeq, "");
            DataTable table = ListSalaryCommand.Tables[0];
            lbTotalTrainee.Text = ListSalaryCommand.Tables[0].Rows.Count.ToString();
            RadGrid2.DataSource = ListSalaryCommand.Tables[0];
            RadGrid2.DataBind();
        }

        protected void RadGrid2_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                int seq = Convert.ToInt32(RadGrid2.Items[e.Item.ItemIndex]["trainee_seq"].Text);
                CDesBLL.DeleteTrainee(seq, ssMng.EmpSeq);
            }
            else if (e.CommandName == "AddData")
            {
                UpdateUI2("AddNew");
                ClearControlData();
            }
            else if (e.CommandName == "DataEdit")
            {
                string TraineeSeq = RadGrid2.Items[e.Item.ItemIndex]["trainee_seq"].Text;
                string strEmp = RadGrid2.Items[e.Item.ItemIndex]["emp_seq"].Text;
                txtTraineeSeq.Text = TraineeSeq;
                cboPerson.SelectedValue = strEmp;
                LoadcbTrainee();
                UpdateUI2("Update");
            }
            LoadDataGrid2();
        }

        protected void RadGrid2_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem item = (GridDataItem)e.Item;
                string strGranted = item["granted"].Text;

                item["ClientSelectColumn"].Enabled = strGranted == "0";
                //if (strGranted == "1")
                //{
                //    item["ClientSelectColumn"].Visible = false;
                //}
                //else
                //{
                //    item["ClientSelectColumn"].Enabled = true;
                //}
            }   
        }
        protected void LoadJobSite()
        {
            IList<JobSiteInfo> infoList = jBLL.ListJobSiteByTypes(SystemInfo._JobsiteInternal, "3");           
            cbJobSite.DataSource = infoList;
            cbJobSite.DataBind();
        }
        protected void btnAddGranted_OnClick(object sender, EventArgs e)
        {

            for (int i = 0; i < RadGrid2.Items.Count; i++)
            {
                GridDataItem item = RadGrid2.Items[i];
               //string comboStatus = cbStatus.SelectedValue;
                //CheckBox chk = (CheckBox)item["ClientSelectColumn"].Controls[0];                              
                //if (chk.Checked)
                if (RadGrid2.Items[i].Selected)
                {
                    string TrainSeq = Request.QueryString["Sequence"].ToString();
                    string trainee_seq = item["trainee_seq"].Text;
                    string emp_seq = item["emp_seq"].Text;
                    string User = ssMng.EmpSeq;
                    string strComment = "";
                    //rblTrainResult.SelectedValue();
                    //txtTrainHour.Text;
                    CDesBLL.InsertQuta(trainee_seq, TrainSeq, emp_seq, strComment, "", txtTrainHour.Text, User, "1");
           
                }
            }
            LoadDataGrid1();
        }
        protected void LoadEmployeeList()
        {
            string strSeq = "";
            Nullable<int> Seq = cn.ConvertNullInt(strSeq);
            DataSet ListSalaryCommand = EmpBLL.LoadEmployees(Seq, "1", true, "");
            DataTable table = ListSalaryCommand.Tables[0];
           cbEmployeeList.DataSource = ListSalaryCommand.Tables[0];
           cbEmployeeList.DataBind();
        }
        protected void rblAddPreson_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (rblAddPreson.SelectedValue == "1")
            {
                //PanelInPerson.Visible = false;
                PanelExPerson.Visible = false;
                PanelJobSie.Visible = true;
            }
            else if (rblAddPreson.SelectedValue == "0")
            {
               //PanelInPerson.Visible = true; 
                PanelExPerson.Visible = true;
                PanelJobSie.Visible = false;
            }
        }
        
        private  void ClearControlData()
        {
            cboPerson.SelectedIndex = 0;
        }

        protected void btnSave2_Click(object sender, EventArgs e)
        {
            if (checkValidate() == true)
            {
                string strJobSite = cbJobSite.SelectedValue;//CDesBLL.GetData(cboPerson.SelectedValue);
                //string strJobSite = ListJobSite.Tables[0];
                string TrainSeq = Request.QueryString["Sequence"];
                string User = ssMng.EmpSeq;
                string JobSiteSeq = "";
                string WorkingName = "";
                string ClassName = "";
                string PostName = "";

                DataSet ListSalaryCommand = EmpBLL.LoadEmployees(cn.ConvertNullInt(cboPerson.SelectedValue), "1", true, "");
                if (ListSalaryCommand.Tables.Count > 0)
                {
                    DataTable table = ListSalaryCommand.Tables[0];
                    JobSiteSeq = table.Rows[0]["JOB_SITE_SEQ"].ToString();
                    WorkingName = table.Rows[0]["WORKING_JOB_SITE_NAME"].ToString();
                    ClassName = table.Rows[0]["CLASS_NAME"].ToString();
                    PostName = table.Rows[0]["POS_NAME"].ToString();
               }



                CDesBLL.InsertByTrainee(TrainSeq, cboPerson.SelectedValue, txtExPerson.Text, JobSiteSeq, WorkingName, PostName, ClassName, User);
                LoadDataGrid2();
                LoadcbTrainee();
                UpdateUI2("Save");
            }
        }
        protected void btnUpdate2_Click(object sender, EventArgs e)
        {
            string strJobSite = cbJobSite.SelectedValue;//CDesBLL.GetData(cboPerson.SelectedValue);
            //string strJobSite = ListJobSite.Tables[0];
            string TrainSeq = Request.QueryString["Sequence"].ToString();
            string User = ssMng.EmpSeq;
            string JobSiteSeq = "";
            string WorkingName = "";
            string ClassName = "";
            string PostName = "";
            DataSet ListSalaryCommand = EmpBLL.LoadEmployees(cn.ConvertNullInt(cboPerson.SelectedValue), "1", true, "");
           if (ListSalaryCommand.Tables.Count > 0)
           {
                DataTable table = ListSalaryCommand.Tables[0];
                JobSiteSeq = table.Rows[0]["JOB_SITE_SEQ"].ToString();
                WorkingName = table.Rows[0]["WORKING_JOB_SITE_NAME"].ToString();
                ClassName = table.Rows[0]["CLASS_NAME"].ToString();
                PostName = table.Rows[0]["POS_NAME"].ToString();
            }

           CDesBLL.UpdateTrainee(Convert.ToInt32(txtTraineeSeq.Text), TrainSeq, cboPerson.SelectedValue, txtExPerson.Text, JobSiteSeq, WorkingName, PostName, ClassName, User);
            LoadDataGrid2();
            LoadcbTrainee();
            UpdateUI2("Save");
        }
        protected void btnCancel2_Click(object sender, EventArgs e)
        {
            UpdateUI2("Cancel");
            ClearControlData();
        }
        protected bool checkValidate()
        {
            bool bOK = true;
            //string strJobSite = cbJobSite.SelectedValue;
            //string strQuota = txtquota.Text;

            //if (strJobSite.Length == 0)
            //{
            //    bOK = false;
            //    lblError2.Text = "* กรุณาเลือกหน่วยงาน";
            //}
            return bOK;
        }
        protected void UpdateUI2(string strCommand)
        {
            switch (strCommand)
            {
                case "Save":
                    PanelGrid2.Visible = true;
                    PanelAddData2.Visible = false;

                    break;

                case "AddNew":
                    PanelGrid2.Visible = false;
                    PanelAddData2.Visible = true;
                    PanelInPerson.Visible = false;
                    pnlPerson.Visible = true;
                    btnUpdate2.Visible = false;
                    btnSave2.Visible = true;

                    break;

                case "Cancel":
                    PanelGrid2.Visible = true;
                    PanelAddData2.Visible = false;

                    break;

                case "Update":
                    PanelGrid2.Visible = false;
                    PanelAddData2.Visible = true;
                    PanelInPerson.Visible = false;
                    pnlPerson.Visible = true;
                    btnSave2.Visible = false;
                    btnUpdate2.Visible = true;

                    break;
            }
        }



        protected void cboPerson_DataBound(object sender, EventArgs e)
        {
            //set the initial footer label
            ((Literal)cboPerson.Footer.FindControl("RadComboItemsCount")).Text = Convert.ToString(cboPerson.Items.Count);
        }

        protected void cboPerson_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
        {
            cboPerson.DataSource = tBLL.LoadPerson();
            cboPerson.DataBind();
        }

        protected void cboPerson_ItemDataBound(object sender, RadComboBoxItemEventArgs e)
        {
            e.Item.Text = ((DataRowView)e.Item.DataItem)["emp_name"].ToString();
            e.Item.Value = ((DataRowView)e.Item.DataItem)["emp_seq"].ToString();
        }

    //======================================== รายชื่อผู้มีสิทธิ์  =================================================
        private void LoadDataGrid1()
        {
           
                string TrainSeq = Request.QueryString["Sequence"].ToString();
                DataSet ListDataCommand = CDesBLL.GetData("", TrainSeq, "1");
                DataTable table = ListDataCommand.Tables[0];
                lbAccepTrainee.Text = ListDataCommand.Tables[0].Rows.Count.ToString();
                RadGrid1.DataSource = ListDataCommand.Tables[0];
                RadGrid1.DataBind();           
        }

        protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                int seq = Convert.ToInt32(RadGrid2.Items[e.Item.ItemIndex]["trainee_seq"].Text);
                CDesBLL.DeleteQuta(seq, ssMng.EmpSeq);
            }
            else if (e.CommandName == "AddData")
            {
                //UpdateUI1("AddNew");
            }
            LoadDataGrid1();
        }

        protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {

        }
        protected void btnSave1_Click(object sender, EventArgs e)
        {
            //if (checkValidate() == true)
            //{
            //    string TrainSeq = Request.QueryString["Sequence"].ToString();
            //    string User = ssMng.EmpSeq;
            //    string strComment = "";
            //    //rblTrainResult.SelectedValue();
            //    //txtTrainHour.Text;
            //    CDesBLL.InsertQuta("", TrainSeq, cbTrainee.SelectedValue, strComment, Convert.ToInt32(rblTrainResult.SelectedValue), txtTrainHour.Text, User, "1");
            //    LoadDataGrid1();
            //    UpdateUI1("Save");
            //}
        }
        protected void btnUpdate1_Click(object sender, EventArgs e)
        {
            //LoadDataGrid1();
            //UpdateUI1("Save");
        }
        protected void btnCancel1_Click(object sender, EventArgs e)
        {
            //UpdateUI1("Cancel");
            //ClearControl1();
        }
        private void LoadcbTrainee()
        {
            string TrainSeq = Request.QueryString["Sequence"].ToString();
            DataSet ListSalaryCommand = CDesBLL.GetData("", TrainSeq, "0");
            DataTable table = ListSalaryCommand.Tables[0];
            cbTrainee.DataSource = ListSalaryCommand.Tables[0];
            cbTrainee.DataBind();
        }
        protected void ClearControl1()
        {
        }
        protected bool checkValidate1()
        {
            bool bOK = true;
            //string strJobSite = cbJobSite.SelectedValue;
            //string strQuota = txtquota.Text;

            //if (strJobSite.Length == 0)
            //{
            //    bOK = false;
            //    lblError2.Text = "* กรุณาเลือกหน่วยงาน";
            //}
            return bOK;
        }

      protected void UpdateUI1(string strCommand)
        {
            switch (strCommand)
            {
                case "Save":
                    PanelGrid1.Visible = true;
                    //PanelAddData1.Visible = false;

                    break;

                case "AddNew":
                    PanelGrid1.Visible = false;
                    PanelAddData1.Visible = true;
                    btnUpdate1.Visible = false;
                    btnSave1.Visible = true;

                    break;

                case "Cancel":
                    PanelGrid1.Visible = true;
                    PanelAddData1.Visible = false;

                    break;

                case "Update":
                    PanelGrid1.Visible = false;
                    PanelAddData1.Visible = true;
                    btnSave1.Visible = false;
                    btnUpdate1.Visible = true;

                    break;
            }
        }

    }
}
